package gui.panel_new;

import enums.FileFilterEnum;
import enums.TextBundleEnum;
import enums.URLEnum;
import j_util.Utiles;
import misc.BundlesProvider;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ResourceBundle;
/*

    This file is part of Logica-Logike.

    Logica-Logike is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation version 3 of the License.

    Logica-Logike is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Logica-Logike.  If not, see <http://www.gnu.org/licenses/>.

 */
/**
 * Panel destinado al aprendizaje de lo referente al cuadro de oposicion;
 *
 * @author Jose Martin Rozanec;
 */
public class OposicionPanel extends OpenSavePanel {

    private javax.swing.JButton aFalsaButton;
    private javax.swing.JLabel aLabel;
    private javax.swing.JButton aVerdaderaButton;
    private javax.swing.JPanel bottomPanel;
    private javax.swing.JLabel cuadroLabel;
    private javax.swing.JPanel cuadroPanel;
    private javax.swing.JButton eFalsaButton;
    private javax.swing.JLabel eLabel;
    private javax.swing.JButton eVerdaderaButton;
    private javax.swing.JLabel fLabel;
    private javax.swing.JButton iFalsaButton;
    private javax.swing.JLabel iLabel;
    private javax.swing.JButton iVerdaderaButton;
    private javax.swing.JButton oFalsaButton;
    private javax.swing.JLabel oLabel;
    private javax.swing.JButton oVerdaderaButton;
    private javax.swing.JPanel referenciaPanel;
    private javax.swing.JButton resetButton;
    private javax.swing.JLabel vLabel;
    private javax.swing.JLabel v_fLabel;
    private ResourceBundle textBundle;

    /**
     * Constructor;
     *
     * @param saveButton;
     * @param openButton;
     * @param fileChooser;
     */
    public OposicionPanel(JButton saveButton, JButton openButton, JFileChooser fileChooser) {
        this.saveButton = saveButton;
        this.openButton = openButton;
        this.fileChooser = fileChooser;
        textBundle = BundlesProvider.getBundle(URLEnum.TEXT_BUNDLE);
        fileFilter = FileFilterEnum.PNG;
        initComponents();
    }

    /**
     * Inicializa los componentes;
     */
    private void initComponents() {
        bottomPanel = new javax.swing.JPanel();
        cuadroPanel = new javax.swing.JPanel();
        cuadroLabel = new javax.swing.JLabel();
        referenciaPanel = new javax.swing.JPanel();
        vLabel = new javax.swing.JLabel();
        fLabel = new javax.swing.JLabel();
        v_fLabel = new javax.swing.JLabel();
        resetButton = new javax.swing.JButton();
        aLabel = new javax.swing.JLabel();
        eLabel = new javax.swing.JLabel();
        iLabel = new javax.swing.JLabel();
        oLabel = new javax.swing.JLabel();
        aVerdaderaButton = new javax.swing.JButton();
        aFalsaButton = new javax.swing.JButton();
        eVerdaderaButton = new javax.swing.JButton();
        eFalsaButton = new javax.swing.JButton();
        iVerdaderaButton = new javax.swing.JButton();
        iFalsaButton = new javax.swing.JButton();
        oVerdaderaButton = new javax.swing.JButton();
        oFalsaButton = new javax.swing.JButton();

        bottomPanel.setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(204, 204, 204), 1, true), "Teoria"), javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED)));

        cuadroPanel.setBackground(new java.awt.Color(255, 255, 255));
        cuadroPanel.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));

        cuadroLabel.setIcon(Utiles.createImageIcon(URLEnum.getReference(URLEnum.RESET_OPOSICION)));

        referenciaPanel.setBackground(new java.awt.Color(255, 255, 255));
        referenciaPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(204, 204, 204), 1, true), "Referencias"));

        vLabel.setIcon(Utiles.createImageIcon(URLEnum.getReference(URLEnum.VERDADERO)));
        fLabel.setIcon(Utiles.createImageIcon(URLEnum.getReference(URLEnum.FALSO)));
        v_fLabel.setIcon(Utiles.createImageIcon(URLEnum.getReference(URLEnum.VERDADERO_FALSO)));

        resetButton.setText(textBundle.getString(TextBundleEnum.RESET.toString()));
        resetButton.setPreferredSize(new Dimension(75, 20));
        resetButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                reset();
            }
        });
        resetButton.setMnemonic(KeyEvent.VK_T);

        aLabel = new JLabel(Utiles.createImageIcon(URLEnum.getReference(URLEnum.OPOSICION_A)));

        eLabel = new JLabel(Utiles.createImageIcon(URLEnum.getReference(URLEnum.OPOSICION_E)));

        iLabel = new JLabel(Utiles.createImageIcon(URLEnum.getReference(URLEnum.OPOSICION_I)));

        oLabel = new JLabel(Utiles.createImageIcon(URLEnum.getReference(URLEnum.OPOSICION_O)));

        aVerdaderaButton.setText(textBundle.getString(TextBundleEnum.VERDADERA.toString()));
        aVerdaderaButton.setPreferredSize(new Dimension(100, 25));
        aVerdaderaButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                setAverdaderaOfalsa(true);
            }
        });
        aVerdaderaButton.setMnemonic('q');

        aFalsaButton.setText(textBundle.getString(TextBundleEnum.FALSA.toString()));
        aFalsaButton.setPreferredSize(new Dimension(100, 25));
        aFalsaButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                setAfalsaOverdadera(true);
            }
        });
        aFalsaButton.setMnemonic('a');

        eVerdaderaButton.setText(textBundle.getString(TextBundleEnum.VERDADERA.toString()));
        eVerdaderaButton.setPreferredSize(new Dimension(100, 25));
        eVerdaderaButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                setEverdaderaIfalsa(true);
            }
        });
        eVerdaderaButton.setMnemonic('w');

        eFalsaButton.setText(textBundle.getString(TextBundleEnum.FALSA.toString()));
        eFalsaButton.setPreferredSize(new Dimension(100, 25));
        eFalsaButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                setEfalsaIverdadera(true);
            }
        });
        eFalsaButton.setMnemonic('s');

        iVerdaderaButton.setText(textBundle.getString(TextBundleEnum.VERDADERA.toString()));
        iVerdaderaButton.setPreferredSize(new Dimension(100, 25));
        iVerdaderaButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                setEfalsaIverdadera(false);
            }
        });
        iVerdaderaButton.setMnemonic('e');

        iFalsaButton.setText(textBundle.getString(TextBundleEnum.FALSA.toString()));
        iFalsaButton.setPreferredSize(new Dimension(100, 25));
        iFalsaButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                setEverdaderaIfalsa(false);
            }
        });
        iFalsaButton.setMnemonic('d');

        oVerdaderaButton.setText(textBundle.getString(TextBundleEnum.VERDADERA.toString()));
        oVerdaderaButton.setPreferredSize(new Dimension(100, 25));
        oVerdaderaButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                setAfalsaOverdadera(false);
            }
        });
        oVerdaderaButton.setMnemonic('r');

        oFalsaButton.setText(textBundle.getString(TextBundleEnum.FALSA.toString()));
        oFalsaButton.setPreferredSize(new Dimension(100, 25));
        oFalsaButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                setAverdaderaOfalsa(false);
            }
        });
        oFalsaButton.setMnemonic('f');

        setBackground(Color.WHITE);
        setLayout(new GridBagLayout());
        GridBagConstraints gridBagConstraints = new GridBagConstraints();
        int column = 0;
        int row = -1;

//        /*  ReferenciaPanel     */
        JPanel refAuxPanel = new JPanel(new GridBagLayout());
        refAuxPanel.setBackground(Color.WHITE);
        referenciaPanel.setLayout(new GridBagLayout());
        gridBagConstraints.ipadx = 10;
        gridBagConstraints.ipady = 10;
        gridBagConstraints.gridy = ++row;
        referenciaPanel.add(vLabel, gridBagConstraints);

        gridBagConstraints.gridy = ++row;
        referenciaPanel.add(fLabel, gridBagConstraints);

        gridBagConstraints.gridy = ++row;
        referenciaPanel.add(v_fLabel, gridBagConstraints);

        gridBagConstraints.gridy = ++row;
        referenciaPanel.add(resetButton, gridBagConstraints);

        row = 0;
        column = 0;
        gridBagConstraints.gridheight = 3;
        gridBagConstraints.gridx = column;
        gridBagConstraints.gridy = row;
        refAuxPanel.add(referenciaPanel, gridBagConstraints);
        row = 4;
        gridBagConstraints.gridheight = 1;
        gridBagConstraints.gridy = row;
        refAuxPanel.add(resetButton, gridBagConstraints);

        row = 0;
        gridBagConstraints.gridx = column;
        gridBagConstraints.gridy = row;
        gridBagConstraints.gridheight = 6;
        gridBagConstraints.gridwidth = 4;
        add(cuadroLabel, gridBagConstraints);

        gridBagConstraints.gridheight = 4;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.gridx = ++column + 4;
        gridBagConstraints.gridy = 2;
        add(refAuxPanel, gridBagConstraints);

        /*  End of referencePanel and cuadro de oposicion */

        /*  Buttons */
        row = 7;
        column = 0;
        gridBagConstraints.gridheight = 1;
        gridBagConstraints.gridwidth = 1;
        gridBagConstraints.gridx = column;
        gridBagConstraints.gridy = row;
        add(aLabel, gridBagConstraints);
        gridBagConstraints.gridx = ++column;
        add(eLabel, gridBagConstraints);
        gridBagConstraints.gridx = ++column;
        add(iLabel, gridBagConstraints);
        gridBagConstraints.gridx = ++column;
        add(oLabel, gridBagConstraints);
        column = 0;
        gridBagConstraints.gridx = column;
        gridBagConstraints.gridy = ++row;
        gridBagConstraints.insets = new Insets(3, 3, 3, 3);

        add(aVerdaderaButton, gridBagConstraints);
        gridBagConstraints.gridx = ++column;
        add(eVerdaderaButton, gridBagConstraints);
        gridBagConstraints.gridx = ++column;
        add(iVerdaderaButton, gridBagConstraints);
        gridBagConstraints.gridx = ++column;
        add(oVerdaderaButton, gridBagConstraints);
        column = 0;
        gridBagConstraints.gridx = column;
        gridBagConstraints.gridy = ++row;

        add(aFalsaButton, gridBagConstraints);
        gridBagConstraints.gridx = ++column;
        add(eFalsaButton, gridBagConstraints);
        gridBagConstraints.gridx = ++column;
        add(iFalsaButton, gridBagConstraints);
        gridBagConstraints.gridx = ++column;
        add(oFalsaButton, gridBagConstraints);

        column = 0;
        gridBagConstraints.gridx = column;
        gridBagConstraints.gridy = ++row;
        gridBagConstraints.gridwidth = 8;
        bottomPanel.setBackground(Color.WHITE);
        bottomPanel.setMinimumSize(new Dimension(655, 100));
        bottomPanel.setPreferredSize(new Dimension(655, 100));
        JTextArea textArea = new JTextArea();
        textArea.setColumns(58);
        textArea.setEditable(false);
        textArea.setRows(5);
//        textArea.setPreferredSize(new Dimension(225,110));
        textArea.setLineWrap(true);
        String theory = "Contradictorias: Se oponen en cuanto la verdad y falsedad.\n" +
                "Contrarias: No pueden ser verdaderas a la vez.\n" +
                "Subcontrarias: No pueden ser falsas a la vez.\n" +
                "Subalternas: Si la universal es verdadera, la particular lo sera tambien. " +
                "Si la particular es falsa, la universal lo \t\t sera tambien.";//todo ver este texto i18n
        textArea.setFont(new java.awt.Font("Tahoma", 1, 11));
        textArea.setText(theory);
        bottomPanel.add(textArea);
        add(bottomPanel, gridBagConstraints);
    }

    /**
     * Resetea el cuadro de oposicion;
     */
    private void reset() {
        aVerdaderaButton.setBackground(new Color(240, 240, 240));
        aFalsaButton.setBackground(new Color(240, 240, 240));
        eVerdaderaButton.setBackground(new Color(240, 240, 240));
        eFalsaButton.setBackground(new Color(240, 240, 240));
        iVerdaderaButton.setBackground(new Color(240, 240, 240));
        iFalsaButton.setBackground(new Color(240, 240, 240));
        oVerdaderaButton.setBackground(new Color(240, 240, 240));
        oFalsaButton.setBackground(new Color(240, 240, 240));
        cuadroLabel.setIcon(Utiles.createImageIcon(URLEnum.getReference(URLEnum.RESET_OPOSICION)));

    }

    /**
     * Establece los valores del cuadro de oposicion si A es falsa u O es verdadera;
     *
     * @param isA - boolean;
     */
    private void setAfalsaOverdadera(boolean isA) {
        aVerdaderaButton.setBackground(new Color(240, 240, 240));
        if (isA) {
            aFalsaButton.setBackground(new Color(51, 255, 153));
        } else {
            aFalsaButton.setBackground(new Color(240, 240, 240));
        }
        eVerdaderaButton.setBackground(new Color(240, 240, 240));
        eFalsaButton.setBackground(new Color(240, 240, 240));
        iVerdaderaButton.setBackground(new Color(240, 240, 240));
        iFalsaButton.setBackground(new Color(240, 240, 240));
        if (!isA) {
            oVerdaderaButton.setBackground(new Color(51, 255, 153));
        } else {
            oVerdaderaButton.setBackground(new Color(240, 240, 240));
        }
        oFalsaButton.setBackground(new Color(240, 240, 240));
        cuadroLabel.setIcon(Utiles.createImageIcon(URLEnum.getReference(URLEnum.A_FALSA_O_VERDADERA)));
    }

    /**
     * Establece los valores del cuadro de oposicion si A es verdadera u O es falsa;
     *
     * @param isA - boolean;
     */
    private void setAverdaderaOfalsa(boolean isA) {
        if (isA) {
            aVerdaderaButton.setBackground(new Color(51, 255, 153));
        } else {
            aVerdaderaButton.setBackground(new Color(240, 240, 240));
        }
        aFalsaButton.setBackground(new Color(240, 240, 240));
        eVerdaderaButton.setBackground(new Color(240, 240, 240));
        eFalsaButton.setBackground(new Color(240, 240, 240));
        iVerdaderaButton.setBackground(new Color(240, 240, 240));
        iFalsaButton.setBackground(new Color(240, 240, 240));
        oVerdaderaButton.setBackground(new Color(240, 240, 240));
        if (!isA) {
            oFalsaButton.setBackground(new Color(51, 255, 153));
        } else {
            oFalsaButton.setBackground(new Color(240, 240, 240));
        }
        cuadroLabel.setIcon(Utiles.createImageIcon(URLEnum.getReference(URLEnum.A_VERDADERA_O_FALSA)));
    }

    /**
     * Establece los valores del cuadro de oposicion si E es verdadera u I es falsa;
     *
     * @param isE - boolean;
     */
    private void setEverdaderaIfalsa(boolean isE) {
        if (isE) {
            eVerdaderaButton.setBackground(new Color(51, 255, 153));
        } else {
            eVerdaderaButton.setBackground(new Color(240, 240, 240));
        }
        aVerdaderaButton.setBackground(new Color(240, 240, 240));
        aFalsaButton.setBackground(new Color(240, 240, 240));
        eFalsaButton.setBackground(new Color(240, 240, 240));
        iVerdaderaButton.setBackground(new Color(240, 240, 240));
        oVerdaderaButton.setBackground(new Color(240, 240, 240));
        oFalsaButton.setBackground(new Color(240, 240, 240));
        if (!isE) {
            iFalsaButton.setBackground(new Color(51, 255, 153));
        } else {
            iFalsaButton.setBackground(new Color(240, 240, 240));
        }
        cuadroLabel.setIcon(Utiles.createImageIcon(URLEnum.getReference(URLEnum.E_VERDADERA_I_FALSA)));
    }

    /**
     * Establece los valores del cuadro de oposicion si E es falsa u I es verdadera;
     *
     * @param isE
     */
    private void setEfalsaIverdadera(boolean isE) {
        eVerdaderaButton.setBackground(new Color(240, 240, 240));
        if (isE) {
            eFalsaButton.setBackground(new Color(51, 255, 153));
        } else {
            eFalsaButton.setBackground(new Color(240, 240, 240));
        }
        aVerdaderaButton.setBackground(new Color(240, 240, 240));
        aFalsaButton.setBackground(new Color(240, 240, 240));
        oVerdaderaButton.setBackground(new Color(240, 240, 240));
        oFalsaButton.setBackground(new Color(240, 240, 240));
        if (!isE) {
            iVerdaderaButton.setBackground(new Color(51, 255, 153));
        } else {
            iVerdaderaButton.setBackground(new Color(240, 240, 240));
        }
        iFalsaButton.setBackground(new Color(240, 240, 240));
        cuadroLabel.setIcon(Utiles.createImageIcon(URLEnum.getReference(URLEnum.E_FALSA_I_VERDADERA)));
    }

    protected void setSaveActionListener() {
        class SaveSylogismAction extends AbstractAction {
            public SaveSylogismAction() {
                super("", Utiles.createImageIcon(URLEnum.getReference(URLEnum.SAVE_AS)));
            }

            public void actionPerformed(ActionEvent e) {
                System.out.println("" + OposicionPanel.this.getLocationOnScreen());
                int x = (int) OposicionPanel.this.getLocationOnScreen().getX();
                int y = (int) OposicionPanel.this.getLocationOnScreen().getY();
                Rectangle screenshot = new Rectangle(x, y, 670, 545);
                BufferedImage image;
                if (Toolkit.getDefaultToolkit().getScreenSize().getWidth() < x + 700 ||
                        Toolkit.getDefaultToolkit().getScreenSize().getHeight() < y + 575 ||
                        y < 0 ||
                        x < 0) {
                    JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),
                            textBundle.getString(TextBundleEnum.WARNING_PANTALLA_OCLUIDA.toString()), "",
                            JOptionPane.WARNING_MESSAGE);
                } else {
                    try {
                        Robot robot = new Robot();
                        image = robot.createScreenCapture(screenshot);
                        int state = fileChooser.showSaveDialog(JOptionPane.getRootFrame());
                        if (state == JFileChooser.APPROVE_OPTION) {
                            File file;
                            if (!fileChooser.getSelectedFile().getAbsolutePath().endsWith(".png")) {
                                file = new File(fileChooser.getSelectedFile().getAbsolutePath() + ".png");
                            } else {
                                file = fileChooser.getSelectedFile();
                            }
                            try {
                                ImageIO.write(image, "png", file);
                            } catch (IOException e1) {
                                e1.printStackTrace();
                            }
                        }
                    } catch (AWTException e1) {
                        e1.printStackTrace();
                    }
                }
            }
        }
        saveButton.setAction(new SaveSylogismAction());
    }

    protected void setSaveButtonEnabled() {
        saveButton.setEnabled(true);
    }

    protected void setOpenActionListener() {
        class OpenOposicionAction extends AbstractAction {
            public OpenOposicionAction() {
                super("", Utiles.createImageIcon(URLEnum.getReference(URLEnum.OPEN)));
            }

            public void actionPerformed(ActionEvent e) {

            }
        }
        openButton.setAction(new OpenOposicionAction());
    }

    protected void setOpenButtonEnabled() {
        openButton.setEnabled(false);
    }

    protected void setFocusOnComponent() {
        //remains unimplemented...
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("se");
        frame.setContentPane(new OposicionPanel(new JButton(), new JButton(), new JFileChooser()));
        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
